using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Text;
[System.Serializable]

public class AchieveStorage : MonoBehaviour
{
	public  AchievementItem[] achievementItems;
	public AllColorEspecialAchieve allColorsAchieve;
	
	public GameObject UnlockAchieve_Prefab;

    public static AchievementItem[] AchievementItems;
    public static AchieveStorage getInstance;
	
	public bool hasEnabled;
	
    void Awake()
    {
        getInstance = this;
    }
	
	public void Start ()
	{
        AchievementItems = achievementItems;
		foreach(AchievementItem ach in AchievementItems)
		{
			ach.count = PlayerPrefs.GetInt (ach.name);	
			ach.Teste();
		}
		allColorsAchieve.Setup();
		
		AchieveStorage.getInstance.LoadAll();		
	}
	public void SaveAll ()
	{
		foreach(AchievementItem ach in AchievementItems)
		{
			PlayerPrefs.SetInt (ach.name, ach.count);
		}
	}
    public void Save(AchievementItem ach) 
    {
        PlayerPrefs.SetInt(ach.name, ach.count);
    }


    public AchievementItem getByName(String name) 
    {
        foreach(AchievementItem ach in AchievementItems)
        {
            if (ach.name == name)
            {
                return ach;
            }   
        }
		
        return null;
    }

	public void LoadAll()
	{
        foreach (AchievementItem ach in AchievementItems)
        {
            ach.count = PlayerPrefs.GetInt(ach.name, 0);

            if(ach.Teste())
			{
				ach.SetCompleted();
			}
        }
	}


    public void Load(AchievementItem ach) 
    {
        ach.count = PlayerPrefs.GetInt(ach.name, 0);
		ach.Teste();	
    }
	
	public void UnlockAchieve(AchievementItem ach, Camera cam)
	{
		if(!ach.completed)
		{
			if(!GameObject.FindGameObjectWithTag("Achievement"))
			{
				Vector3 pos = new Vector3(cam.transform.position.x , cam.transform.position.y - 5 , cam.transform.position.z + 2);
				GameObject unlockPrefab =  Instantiate(this.UnlockAchieve_Prefab, pos, UnlockAchieve_Prefab.transform.rotation) as GameObject;
				unlockPrefab.transform.parent = cam.transform;
							
				UnlockAchieve_PrefabSc script = unlockPrefab.GetComponent<UnlockAchieve_PrefabSc>();
				ach.SetCompleted();
				script.SetAchieve(ach,script);
			}
			else
			{
				GameObject.FindGameObjectWithTag("Achievement").SendMessage("NextDelete",ach,SendMessageOptions.DontRequireReceiver);
				GameObject.FindGameObjectWithTag("Achievement").SendMessage("receivedPrefab",UnlockAchieve_Prefab.transform,SendMessageOptions.DontRequireReceiver);
			}
		}
	}
		
}

